﻿package hejie.admin.dao.impl;

import hejie.admin.dao.EmployeeDao;

import java.sql.SQLException;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Component;

import publics.model.Employee;

/**
 * 员工持久层实现
 * 
 */
@Component("employeeDao")
public class EmployeeDaoImpl implements EmployeeDao {
	HibernateTemplate hibernateTemplate;// 注入hibernate的spring代理模版

	public HibernateTemplate getHibernateTemplate() {
		return hibernateTemplate;
	}

	// 注入 hibernate模版
	@Resource(name = "hibernateTemplate")	
	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
		System.out.println("hibernateTemplate injected in  EmployeeDaoImpl.");
		// 测试用，实际运行中可去掉这句
	}
	 

	public Integer save(Employee employee){
		System.out.println("EmployeeDaoImpl里保存成功");
		return (Integer)this.getHibernateTemplate().save(employee);
		
	}
	
	public boolean update(Employee employee){
		try{
			this.getHibernateTemplate().update(employee);
		}catch (DataAccessException e) {
			return false;
		}
		return true;
	}
	
    public boolean deleteById(Integer id){
    	try{			
			this.getHibernateTemplate().delete(this.getHibernateTemplate().get(Employee.class, id));
			System.out.println("EmployeeDaoImpl里 id="+id);
		}catch (DataAccessException e) {
			return false;
		}
		return true;
	}
    
	public boolean delete(Employee employee){
		try {
			 this.getHibernateTemplate().delete(employee);
		} catch (DataAccessException e) {
			return false;
		}
	    return true;
	    	
	}
	
	public Employee findById(Integer id){		
		return this.getHibernateTemplate().get(Employee.class, id);
	}
	
	@SuppressWarnings("unchecked")
	public List<Employee> findByStaffNo(String staffNo) {
		return this.getHibernateTemplate().find("from Employee e where e.staffNo = ? " , staffNo);
	}	 
	
    @SuppressWarnings("unchecked")
	public Employee findByUserAndPass(String user,String pass) {
		Object[] args = {user,pass};
	    List lists = getHibernateTemplate().find("from Employee e where e.loginUser = ? and e.loginPass = ?" , args);           
	    if(lists.size() == 0){
	    	return null;
	    }else{
	    	return (Employee)lists.get(0);
	    }
	}
    
   @SuppressWarnings("unchecked")
   public List<Employee> findByPage(final String hql, final int offset, final int pageSize) {
		List<Employee> list = this.getHibernateTemplate().executeFind(
				new HibernateCallback() {
					public Object doInHibernate(Session session) throws HibernateException ,SQLException {
						//执行hibernate分页查询
						List<Employee> result = session.createQuery(hql)
							.setFirstResult(offset)
							.setMaxResults(pageSize)
							.list();
						return result;
					}
				}
				);
		
		return list;
	}
   
	public int findByCount(String sql) {	
		return this.getHibernateTemplate().find(sql).size();
	}

	
	 
}
